//
//  GL_TestCollision.m
//  RecipeCollection02
//
//  Created by Louis CHU on 27/02/12.
//  Copyright (c) 2012 Nathan Burba. All rights reserved.
//

#import "GL_TestCollision.h"

#import "GL_TC_Tank.h"
#import "GL_SteeringBehavior.h"
#import "GL_TransformationCoordonation.h"


#import "Recipe.h"
#import "GameArea2D.h"
#import "GameObject.h"
#import "GameMisc.h"
#import "GameSensor.h"

#import "b2Body.h"

#import "IsoDebugDrawNode.h"

static GL_TestCollision * sSharedInstance;

@interface   GL_TestCollision ()

@property (nonatomic, strong) GL_TC_Tank * tank1;
@property (nonatomic, strong) GL_TC_Tank * tank2;


@end

@implementation GL_TestCollision
@synthesize tank1, tank2;
+ (GL_TestCollision *) sharedInstance
{
    return sSharedInstance;
}


- (CCLayer *) runRecipe
{
    [super runRecipe];
    sSharedInstance = self;
    
    world->SetGravity(b2Vec2(0,0));
    self.tank1 =  [GL_TC_Tank createAtPosition:CGPointMake(100,240)];
    self.tank2 =  [GL_TC_Tank createAtPosition:CGPointMake(76,120)];
    __unused GL_TC_Tank * tank3 =  [GL_TC_Tank createAtPosition:CGPointMake(120,60)];
    
    NSLog(@"%f %f",self.tank1.position.x,self.tank1.position.y);

    //[[planets objectForKey:@"mercury.png"] body]->ApplyLinearImpulse(b2Vec2(0,0.075f), [[planets objectForKey:@"mercury.png"] body]->GetPosition());

    //[tank1 body]->ApplyLinearImpulse(b2Vec2(0,-5.f), [tank1 body]->GetPosition());
    //[tank1 body]->SetZVelocity(20.f);
    //[tank1 body]->SetLinearVelocity(b2Vec2(0.f,-5.f));
    
    //self.tank2.body->SetLinearVelocity(b2Vec2(5.f,0.f));
    
    
    b2Vec2 vector = b2Vec2(10.f,0.f);
    b2Vec2 newVector = [GL_TransformationCoordonation transformTarget:vector  axisDirection:b2Vec2(1,2) fromOrigin:b2Vec2(0.f,0.f)  toOrigin:b2Vec2(1.f,3.f)];
    NSLog(@"%f %f",newVector.x, newVector.y);
    b2Vec2 fromOriginInNewCoord = [GL_TransformationCoordonation transformTarget:b2Vec2(0.f,0.f)  axisDirection:b2Vec2(1,2) fromOrigin:b2Vec2(0.f,0.f)  toOrigin:b2Vec2(1.f,3.f)];
    b2Vec2 oldVector = [GL_TransformationCoordonation transformTarget:newVector  axisDirection:b2Vec2(1,-2) fromOrigin:b2Vec2(0.f,0.f)  toOrigin:fromOriginInNewCoord];
    NSLog(@"%f %f",oldVector.x, oldVector.y);
    
    self.tank1.body->SetLinearVelocity (b2Vec2(0,-0.1f));
    
    return self;
}

- (void) step:(ccTime)dt
{
    [super step:dt];
    //[self.tank2 body]->ApplyForce(b2Vec2(0.5,0), self.tank2.body->GetPosition());
    self.tank1.body->SetLinearVelocity (b2Vec2(0,-0.1f));
    //b2Vec2 steeringSeek = [[GL_SteeringBehavior defaultSteeringBehavior] agent:self.tank1 seekTarget:self.tank2.position];
    b2Vec2 steeringAvoid = [[GL_SteeringBehavior defaultSteeringBehavior] agent:self.tank1 avoidObstacle:self.tank2];
    steeringAvoid = b2Vec2(steeringAvoid.x/(PTM_RATIO),steeringAvoid.y/(PTM_RATIO));
    b2Vec2 direction = b2Vec2(0,-20.f);
    
    [self.tank1 body]->ApplyForce( steeringAvoid, [self.tank1 body]->GetPosition());
    //b2Vec2 velocity = self.tank1.body->GetLinearVelocity();
    //self.tank1.body->SetLinearVelocity(velocity+ steeringAvoid);
    
    [self showDebugDraw];
}


- (b2World *) world
{
    return world;
}

- (CCNode *) gameNode
{
    return gameNode;
}


@end
